<?php
/**
 * Created by PhpStorm.
 * User: qishengqiang
 * Date: 2022-06-02
 * Time: 22:41
 */

namespace app\services;


use app\models\OrganizationDonates;
use app\models\Organizations;
use app\models\UserCreditRecords;
use app\models\UserDonateRecords;
use app\models\UserDonateRecordsDetail;
use app\models\Users;
use think\facade\Db;

class UserCreditService extends ServiceBase
{
    /**
     * @param Users $user
     * @param $credit
     * @param $remark
     * @throws \Exception
     */
    public function add(Users $user, $credit, $remark)
    {
        try {
            $record = new UserCreditRecords();
            $record->userId = $user->userId;
            $record->originCredit = $user->currentCredit;
            $record->changeCredit = $credit;
            $record->finalCredit = $user->currentCredit + $credit;
            $record->remark = $remark;
            $record->save();

            $user->totalCredit += $credit;
            $user->currentCredit += $credit;
            $user->save();

        } catch (\Exception $e) {
            Db::rollback();
            throw new \Exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * @param $user
     * @param $credit
     * @param $remark
     * @throws \Exception
     */
    public function reduce($user, $credit, $remark)
    {
        try {
            $record = new UserCreditRecords();
            $record->userId = $user->userId;
            $record->originCredit = $user->currentCredit;
            $record->changeCredit = intval('-'.$credit);
            $record->finalCredit = $user->currentCredit + $credit;
            $record->remark = $remark;
            $record->save();

            $user->totalCredit -= $credit;
            $user->currentCredit -= $credit;
            $user->save();

        } catch (\Exception $e) {
            throw new \Exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * @param Users $user
     * @param $credit
     * @return bool
     * @throws \Exception
     */
    public function checkDonateCredit(Users $user, $credit, $organizationId)
    {
        try {
            if (!is_int($credit)) {
                throw new \Exception('捐赠积分请输入整数', 500);
            }

            $availCredit = $user->totalCredit - $user->donateCredit;
            if ($availCredit < $credit) {
                throw new \Exception('捐赠积分不能大于可捐赠积分');
            }

            $record = UserDonateRecords::where(['userId' => $user->userId])->find();

            if ($record && $record->organizationId != $organizationId) {
                throw new \Exception('不能助力多个组织');
            }

            return true;

        } catch (\Exception $e) {
            throw new \Exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * @param Organizations $organization
     * @param Users $user
     * @param $credit
     * @return bool
     * @throws \Exception
     */
    public function donate(Organizations $organization, Users $user, $credit)
    {
        Db::startTrans();
        try {
            //1. 组织积分变动
            $organization->creditCount += $credit;
            $organization->creditCountView += $credit;
            $organization->personCount += 1;
            $organization->personCountView += 1;
            $organization->save();

            //2. 组织捐赠积分记录变动
            $donate = OrganizationDonates::where(['organizationId' => $organization->organizationId])->find();
            if (!$donate) {
                $donate = new OrganizationDonates();
                $donate->organizationId = $organization->organizationId;
                $donate->userId = $user->userId;
            }

            $donate->donateCredit += $credit;
            $donate->save();

            //3. 个人积分变动
            $user->donateCredit += $credit;
            $user->save();

            //4. 个人捐赠记录变动
            $record = UserDonateRecords::where(['userId' => $user->userId, 'organizationId' => $organization->organizationId])->find();
            if (!$record) {
                $record = new UserDonateRecords();
                $record->userId = $user->userId;
                $record->organizationId = $organization->organizationId;
            }

            $record->donateCredit += $credit;
            $record->save();

            //4. 个人捐赠记录变动
            $record = new UserDonateRecordsDetail();
            $record->userId = $user->userId;
            $record->organizationId = $organization->organizationId;
            $record->donateCredit = $credit;
            $record->save();

            Db::commit();

        } catch (\Exception $e) {
            Db::rollback();
            throw new \Exception($e->getMessage(), $e->getCode());
        }

    }

    /**
     * @param $user
     * @param $credit
     * @param $remark
     * @throws \Exception
     */
    public function consume($user, $credit, $remark)
    {
        try {
            $record = new UserCreditRecords();
            $record->userId = $user->userId;
            $record->originCredit = $user->currentCredit;
            $record->changeCredit = intval('-'.$credit);
            $record->finalCredit = $user->currentCredit + $credit;
            $record->remark = $remark;
            $record->save();

            $user->currentCredit -= $credit;
            $user->save();

        } catch (\Exception $e) {
            throw new \Exception($e->getMessage(), $e->getCode());
        }
    }
}